package com.person.sys.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import com.person.commons.struts2.BaseAction;
import com.person.commons.utils.Constants;
import com.person.commons.utils.FlipPageInfo;
import com.person.sys.model.SysWeb;
import com.person.sys.service.SysWebService;

public class SysWebAction extends BaseAction{
	
	
	private SysWeb sysWeb;
	private SysWebService sysWebService;
	private String image;
	private String imageFileName;
	private String image1;
	private String image8;
	private String image1FileName;
	private String image8FileName;
	private FlipPageInfo flipData=new FlipPageInfo();
	
	public String index(){
		
		if(sysWebService.findSysWebCount()==1){
			sysWeb=sysWebService.findSysWeb();
			return "edit";
		}else if(sysWebService.findSysWebCount()>1){
			info="宸茬粡鏈夎褰曞瓨鍦紝鍙兘鏈変竴鏉¤褰�";
			return "message";
		}
		else{
			return "create";
		}
	}
	//淇濆瓨鍩烘湰淇℃伅椤甸潰鍐呭锛岃烦杞埌寮瑰嚭绐楀彛椤甸潰
	public String saveWeb1(){
		
	
		if(image1!=null&&!"".equals(image1)){
			sysWeb.setWebLogo(upload(image1,image1FileName));
		}
		return "create1";
		
	}
	//淇濆瓨缃戠珯淇℃伅
	public String saveWeb(){
		
		if(image8!=null&&!"".equals(image8)){
			sysWeb.setWebPopPic(upload(image8,image8FileName));
		}
		sysWebService.saveWeb(sysWeb);
		return index();
	}
	public String list(){
		
		flipData=sysWebService.findSysWebList(flipData);
		return "list";
	}
	@SuppressWarnings("deprecation")
	public String delete(){
		
		sysWeb=sysWebService.findSysWebById(sysWeb.getWebId());
		//鍒犻櫎瀵瑰簲鏈嶅姟鍣ㄤ笂鍥剧墖鏂囦欢
			delFile(request.getRealPath(File.separator)+Constants.WEB_DIR+sysWeb.getWebLogo());
			delFile(request.getRealPath(File.separator)+Constants.WEB_DIR+sysWeb.getWebPopPic());
		sysWebService.deleteWeb(sysWeb);
		return index();
	}
	
	public String saveModify1(){
		
		if(image1!=null&&!"".equals(image1)){
			String filePathAndName=request.getRealPath(File.separator)+Constants.WEB_DIR+sysWeb.getWebLogo();
			delFile(filePathAndName);
			sysWeb.setWebLogo(upload(image1,image1FileName));
		}
		sysWebService.updateWeb(sysWeb);
		return "edit1";
	}
	public String saveModify(){
		
		if(image8!=null&&!"".equals(image8)){
			String filePathAndName=request.getRealPath(File.separator)+Constants.WEB_DIR+sysWeb.getWebPopPic();
			delFile(filePathAndName);
			sysWeb.setWebPopPic(upload(image8,image8FileName));
		}
		sysWebService.updateWeb(sysWeb);
		return index();
	}
	public String ModifyPop(){
		
		sysWeb=sysWebService.findSysWebById(sysWeb.getWebId());
		return "edit1";
	}
	/**
	 * 鍥剧墖鏂囦欢涓婁紶
	 * @return
	 */
	
	@SuppressWarnings("deprecation")
	public String upload(String image,String imageFileName) {
		
		String imageType = imageFileName.substring(
				imageFileName.lastIndexOf("."));
		String rootDir = request.getRealPath(File.separator)
				+ Constants.WEB_DIR;
		String webPicName = String.valueOf(new Date().getTime());
		String imageDir = rootDir + webPicName + imageType;

		File rootFile = new File(rootDir);
		File imageFile = new File(imageDir);
		if (!rootFile.exists()) {
			rootFile.mkdirs();
		}

		if (imageFile.exists()) {
			imageFile.delete();
		}

		try {
			FileInputStream is = new FileInputStream(image);
			FileOutputStream os = new FileOutputStream(imageDir);
			byte[] buffer = new byte[4096];
			int lenth = 0;
			while ((lenth = is.read(buffer)) > 0) {
				os.write(buffer);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String fileType=webPicName + imageType;
		return fileType;
	}

	/**
	  * 鍒犻櫎鍦ㄦ湇鍔″櫒涓婄殑鏂囦欢
	  * @param filePathAndName
	  */
	public void delFile(String filePathAndName) { 
		
	     try { 
	       String filePath = filePathAndName; 
	       filePath = filePath.toString(); 
	       File myDelFile = new File(filePath); 
	       myDelFile.delete(); 
	     } 
	     catch (Exception e) { 
	       e.printStackTrace(); 
	     } 
	 } 


	/**
	 * @return the sysWeb
	 */
	public SysWeb getSysWeb() {
		return sysWeb;
	}
	/**
	 * @param sysWeb the sysWeb to set
	 */
	public void setSysWeb(SysWeb sysWeb) {
		this.sysWeb = sysWeb;
	}
	/**
	 * @return the image
	 */
	public String getImage() {
		return image;
	}
	/**
	 * @param image the image to set
	 */
	public void setImage(String image) {
		this.image = image;
	}
	/**
	 * @return the imageFileName
	 */
	public String getImageFileName() {
		return imageFileName;
	}
	/**
	 * @param imageFileName the imageFileName to set
	 */
	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}
	/**
	 * @param sysWebService the sysWebService to set
	 */
	public void setSysWebService(SysWebService sysWebService) {
		this.sysWebService = sysWebService;
	}
	/**
	 * @return the image1
	 */
	public String getImage1() {
		return image1;
	}
	/**
	 * @param image1 the image1 to set
	 */
	public void setImage1(String image1) {
		this.image1 = image1;
	}
	/**
	 * @return the image8
	 */
	public String getImage8() {
		return image8;
	}
	/**
	 * @param image8 the image8 to set
	 */
	public void setImage8(String image8) {
		this.image8 = image8;
	}
	/**
	 * @return the image1FileName
	 */
	public String getImage1FileName() {
		return image1FileName;
	}
	/**
	 * @param image1FileName the image1FileName to set
	 */
	public void setImage1FileName(String image1FileName) {
		this.image1FileName = image1FileName;
	}
	/**
	 * @return the image8FileName
	 */
	public String getImage8FileName() {
		return image8FileName;
	}
	/**
	 * @param image8FileName the image8FileName to set
	 */
	public void setImage8FileName(String image8FileName) {
		this.image8FileName = image8FileName;
	}
	/**
	 * @return the flipData
	 */
	public FlipPageInfo getFlipData() {
		return flipData;
	}
	/**
	 * @param flipData the flipData to set
	 */
	public void setFlipData(FlipPageInfo flipData) {
		this.flipData = flipData;
	}
	
}
